package com.ourteam.workspace.dao.impl;

import com.ourteam.workspace.dao.IWorkspaceProductDatabaseRelateDAO;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseRelate;
import com.ourteam.workspace.dao.WorkspaceProductDatabaseRelateDAOQueryBean;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;

/**
 * Template Name : DAO服务对象 Template Version : 1 Template Last Modfiy Date : Fri Nov 17 11:00:11 CST
 * 2017 Auto Generate Date : 2018-11-30 18:42:35 Warning ! This file will be overwrite when
 * regenerate !
 */
public class AbstractWorkspaceProductDatabaseRelateDAOJdbcImpl extends AbstractSpringDAO
    implements IWorkspaceProductDatabaseRelateDAO {

  public static final String[] ENTITY_FIELDS =
      new String[] {
        Id,
        WorkspaceId,
        ProductDatabaseId,
        RelateName,
        Status,
        Remarks,
        DatabaseName,
        DatabaseType,
        DatabaseIp,
        DatabasePort,
        DatabaseServiceId
      };

  public static final String[] ENTITY_PROPERTIES =
      new String[] {
        _DataObjectProperty.Id,
        _DataObjectProperty.WorkspaceId,
        _DataObjectProperty.ProductDatabaseId,
        _DataObjectProperty.RelateName,
        _DataObjectProperty.Status,
        _DataObjectProperty.Remarks,
        _DataObjectProperty.DatabaseName,
        _DataObjectProperty.DatabaseType,
        _DataObjectProperty.DatabaseIp,
        _DataObjectProperty.DatabasePort,
        _DataObjectProperty.DatabaseServiceId
      };

  public static final String[] ENTITY_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] {Id};

  public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {};

  private static String relatedJoinSQL = null;

  private static String basicFilterSQL = null;

  private DataAccessObjectHelper<
          WorkspaceProductDatabaseRelate, WorkspaceProductDatabaseRelateDAOQueryBean>
      daoHelper;

  public AbstractWorkspaceProductDatabaseRelateDAOJdbcImpl() {
    super();
    daoHelper =
        new DataAccessObjectHelper<
            WorkspaceProductDatabaseRelate, WorkspaceProductDatabaseRelateDAOQueryBean>(this);
  }

  protected String getEntityName() {
    return EntityName;
  }

  protected String[] getEntityFields() {
    return ENTITY_FIELDS;
  }

  protected String[] getEntityProperties() {
    return ENTITY_PROPERTIES;
  }

  protected String[] getEntityPKFields() {
    return ENTITY_PK_FIELDS;
  }

  protected String[] getEntityAutoGenPKFields() {
    return ENTITY_AUTO_GEN_PK_FIELDS;
  }

  protected String[] getEntityUniqueFields() {
    return ENTITY_UNIQUE_FIELDS;
  }

  protected String getBaiscQueryFilter() {

    if (basicFilterSQL == null) {
      StringBuffer basicFilterSQLbuffer = new StringBuffer();

      basicFilterSQL = basicFilterSQLbuffer.toString();
    }

    return basicFilterSQL;
  }

  protected String getRelatedJoinSQL() {

    if (relatedJoinSQL == null) {
      StringBuffer relatedJoinSQLBuffer = new StringBuffer();

      relatedJoinSQLBuffer
          .append(" LEFT JOIN ")
          .append(com.ourteam.product.model.dao.IProductDatabaseDAO.EntityName)
          .append(" RELATED_55VHSO_ProductDatabase ");

      relatedJoinSQLBuffer.append(" ON ( ");

      relatedJoinSQLBuffer.append(
          StringUtils.replace(
              com.ourteam.product.model.dao.IProductDatabaseDAO.Id,
              com.ourteam.product.model.dao.IProductDatabaseDAO.EntityName + ".",
              "RELATED_55VHSO_ProductDatabase."));
      relatedJoinSQLBuffer.append(" = ").append(ProductDatabaseId);

      relatedJoinSQLBuffer.append(" ) ");

      relatedJoinSQL = relatedJoinSQLBuffer.toString();
    }

    return relatedJoinSQL;
  }

  /**
   * Check Data Unique
   *
   * @param aWorkspaceProductDatabaseRelate
   * @throws Exception
   */
  public boolean checkIsUnique(WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate)
      throws Exception {

    if (aWorkspaceProductDatabaseRelate == null) {
      return true;
    }

    List valueList = new ArrayList();

    List pkValueList = new ArrayList();

    pkValueList.add(aWorkspaceProductDatabaseRelate.getId());

    return isUnique(
        valueList.toArray(new Object[valueList.size()]),
        pkValueList.toArray(new Object[pkValueList.size()]));
  }

  /**
   * Insert Data
   *
   * @param aWorkspaceProductDatabaseRelate
   * @throws Exception
   */
  public void insert(WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate)
      throws Exception {

    super.doInsert(aWorkspaceProductDatabaseRelate);
  }

  /**
   * Batch Insert Datas
   *
   * @param aWorkspaceProductDatabaseRelate
   * @throws Exception
   */
  public void batchInsert(WorkspaceProductDatabaseRelate[] aWorkspaceProductDatabaseRelates)
      throws Exception {

    super.doBatchInsert(aWorkspaceProductDatabaseRelates);
  }

  /**
   * Delete Data
   *
   * @param final long id *
   * @throws Exception
   */
  public void delete(final long id) throws Exception {

    super.doDeleteById(new Object[] {new Long(id)});
  }

  /**
   * Delete Datas
   *
   * @param queryBean
   * @throws Exception
   */
  public void delete(WorkspaceProductDatabaseRelateDAOQueryBean queryBean) throws Exception {

    super.doDeleteByQuery(queryBean);
  }

  public void delete(WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate)
      throws Exception {

    delete(aWorkspaceProductDatabaseRelate.getId());
  }

  /**
   * Update Data Selective
   *
   * @param aWorkspaceProductDatabaseRelate
   * @throws Exception
   */
  public void update(WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate)
      throws Exception {

    if (aWorkspaceProductDatabaseRelate.getId() == 0) {
      throw new Exception("Can not update object data whith out primary key value");
    }

    super.doUpdate(aWorkspaceProductDatabaseRelate);

    WorkspaceProductDatabaseRelate tempObj = findById(aWorkspaceProductDatabaseRelate.getId());

    if (tempObj != null) {
      PropertyUtils.copyProperties(aWorkspaceProductDatabaseRelate, tempObj);
    }
  }

  /**
   * Batch Update Datas
   *
   * @param aWorkspaceProductDatabaseRelates[]
   * @throws Exception
   */
  public void batchUpdate(WorkspaceProductDatabaseRelate[] aWorkspaceProductDatabaseRelates)
      throws Exception {

    super.doBatchUpdate(aWorkspaceProductDatabaseRelates);
  }

  /**
   * Update Data By Condictions
   *
   * @param aWorkspaceProductDatabaseRelate
   * @param aValueCriteria
   * @param aTransaction
   * @throws Exception
   */
  public void update(
      WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate,
      WorkspaceProductDatabaseRelateDAOQueryBean aQueryBean)
      throws Exception {

    super.doUpdateByQuery(aWorkspaceProductDatabaseRelate, aQueryBean);
  }

  /**
   * Update Data By Condictions
   *
   * @param aQueryBean
   * @throws Exception
   */
  public void update(WorkspaceProductDatabaseRelateDAOQueryBean aQueryBean) throws Exception {
    super.doUpdateByQuery(aQueryBean);
  }

  protected void checkNullValue(WorkspaceProductDatabaseRelate aWorkspaceProductDatabaseRelate)
      throws Exception {}

  /**
   * Get Data By ID
   *
   * @param final long id * @return ${businessObject.name}
   * @throws Exception
   */
  public WorkspaceProductDatabaseRelate findById(final long id) throws Exception {

    WorkspaceProductDatabaseRelateDAOQueryBean queryBean =
        new WorkspaceProductDatabaseRelateDAOQueryBean();

    WorkspaceProductDatabaseRelateDAOQueryBean.Criteria criteria = queryBean.createCriteria();

    criteria.andIdEqualTo(id);

    // queryBean.setDistinct(true);

    WorkspaceProductDatabaseRelate[] WorkspaceProductDatabaseRelates = query(queryBean);

    if (WorkspaceProductDatabaseRelates.length == 0) {
      return null;
    } else {
      return WorkspaceProductDatabaseRelates[0];
    }
  }

  /**
   * Query Data
   *
   * @param queryBean
   * @return WorkspaceProductDatabaseRelate[]
   * @throws Exception
   */
  public WorkspaceProductDatabaseRelate[] query(
      WorkspaceProductDatabaseRelateDAOQueryBean queryBean) throws Exception {

    List result = super.doQuery(queryBean, new JdbcWorkspaceProductDatabaseRelateRowMapper());

    return (WorkspaceProductDatabaseRelate[])
        result.toArray(new WorkspaceProductDatabaseRelate[result.size()]);
  }

  /**
   * Query ${businessObject.name} Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] querySelective(WorkspaceProductDatabaseRelateDAOQueryBean queryBean)
      throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {
      queryBean.addAllSelectProperties();
    }

    return super.doQuerySelective(queryBean);
  }

  /**
   * Query Data Count
   *
   * @param queryBean
   * @return int
   * @throws Exception
   */
  public int queryCount(WorkspaceProductDatabaseRelateDAOQueryBean queryBean) throws Exception {

    return super.doQueryCount(queryBean);
  }

  public void synchronize(
      WorkspaceProductDatabaseRelate[] aWorkspaceProductDatabaseRelates,
      WorkspaceProductDatabaseRelateDAOQueryBean queryBean)
      throws Exception {
    daoHelper.synchronize(aWorkspaceProductDatabaseRelates, queryBean);
  }

  public void synchronize(
      WorkspaceProductDatabaseRelate[] aWorkspaceProductDatabaseRelates,
      WorkspaceProductDatabaseRelateDAOQueryBean queryBean,
      net.dao.IDataAccessInterface.ISynchronizeCallback<WorkspaceProductDatabaseRelate> callBack)
      throws Exception {
    daoHelper.synchronize(aWorkspaceProductDatabaseRelates, queryBean, callBack);
  }

  private com.ourteam.workspace.dao.impl.AbstractWorkspaceProductDatabaseModelRelateDAOJdbcImpl
      WorkspaceProductDatabaseModelRelateDao;

  public void setWorkspaceProductDatabaseModelRelateDao(
      com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
          WorkspaceProductDatabaseModelRelateDao) {
    this.WorkspaceProductDatabaseModelRelateDao =
        (com.ourteam.workspace.dao.impl.AbstractWorkspaceProductDatabaseModelRelateDAOJdbcImpl)
            WorkspaceProductDatabaseModelRelateDao;
  }

  /**
   * Query Related WorkspaceProductDatabaseModelRelate
   *
   * @param queryBean
   * @throws Exception
   */
  public com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate[]
      queryWorkspaceProductDatabaseModelRelate(WorkspaceProductDatabaseRelateDAOQueryBean queryBean)
          throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
                .WorkspaceProductDatabaseRelateId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.EntityName
            + " RELATED_L27TJS_WorkspaceProductDatabaseModelRelate";

    String[] entityFields =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.ENTITY_FIELDS;

    String[] entityProperties =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.ENTITY_PROPERTIES;

    List list =
        WorkspaceProductDatabaseModelRelateDao.doQuery(
            entityName,
            entityFields,
            entityProperties,
            queryBean,
            joinSQL.toString(),
            new com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO
                .JdbcWorkspaceProductDatabaseModelRelateRowMapper());

    return (com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate[])
        list.toArray(
            new com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate[list.size()]);
  }

  /**
   * Query Related WorkspaceProductDatabaseModelRelate Selective
   *
   * @param queryBean
   * @return Map[]
   * @model
   * @throws Exception
   */
  public Map<String, Object>[] queryWorkspaceProductDatabaseModelRelateSelective(
      WorkspaceProductDatabaseRelateDAOQueryBean queryBean) throws Exception {

    if (queryBean.getSelectProperties().isEmpty()) {

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.Id, "id");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
              .WorkspaceProductDatabaseRelateId,
          "workspaceProductDatabaseRelateId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.ModelPcakageId,
          "modelPcakageId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.LastUpdateTime,
          "lastUpdateTime");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.SubmitterId,
          "submitterId");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.SubmitterName,
          "submitterName");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.Status, "status");

      queryBean.addSelectProperty(
          com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO.Remarks, "remarks");
    }

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
                .WorkspaceProductDatabaseRelateId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.EntityName
            + " RELATED_L27TJS_WorkspaceProductDatabaseModelRelate";

    return WorkspaceProductDatabaseModelRelateDao.doQuerySelective(
        entityName, queryBean, joinSQL.toString());
  }

  /**
   * Update Related WorkspaceProductDatabaseModelRelate
   *
   * @param aWorkspaceProductDatabaseModelRelate
   * @param aQueryBean
   * @model
   * @throws Exception
   */
  public void updateWorkspaceProductDatabaseModelRelate(
      com.ourteam.workspace.dao.WorkspaceProductDatabaseModelRelate
          aWorkspaceProductDatabaseModelRelate,
      WorkspaceProductDatabaseRelateDAOQueryBean aQueryBean)
      throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
                .WorkspaceProductDatabaseRelateId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.EntityName
            + " RELATED_L27TJS_WorkspaceProductDatabaseModelRelate";

    String[] entityFields =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.ENTITY_FIELDS;

    String[] entityProperties =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO.ENTITY_PROPERTIES;

    WorkspaceProductDatabaseModelRelateDao.doUpdateByQuery(
        entityName,
        entityFields,
        entityProperties,
        joinSQL.toString(),
        aWorkspaceProductDatabaseModelRelate,
        aQueryBean);
  }

  /**
   * Delete Related WorkspaceProductDatabaseModelRelate
   *
   * @param aQueryBean
   * @model
   * @throws Exception
   */
  public void deleteWorkspaceProductDatabaseModelRelate(
      WorkspaceProductDatabaseRelateDAOQueryBean aQueryBean) throws Exception {

    StringBuffer joinSQL = new StringBuffer();

    joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

    joinSQL
        .append(
            com.ourteam.workspace.dao.IWorkspaceProductDatabaseModelRelateDAO
                .WorkspaceProductDatabaseRelateId)
        .append(" = ")
        .append(Id);

    joinSQL.append(" ) ");

    String entityName =
        com.ourteam.workspace.dao.impl.WorkspaceProductDatabaseModelRelateDAO
            .EntityName; // + " RELATED_L27TJS_WorkspaceProductDatabaseModelRelate";

    WorkspaceProductDatabaseModelRelateDao.doDeleteByQuery(
        entityName, joinSQL.toString(), aQueryBean);
  }

  public static class JdbcWorkspaceProductDatabaseRelateRowMapper
      implements RowMapper<WorkspaceProductDatabaseRelate> {

    public WorkspaceProductDatabaseRelate mapRow(ResultSet resultSet, int arg1)
        throws SQLException {
      WorkspaceProductDatabaseRelate object = new WorkspaceProductDatabaseRelate();

      int columnIndex = 1;

      int __dataType;

      try {

        Object id = null;

        id = resultSet.getObject(columnIndex++);

        object.setIdNull(resultSet.getBoolean(columnIndex++));

        if (id != null) {

          setBeanValue(object, _DataObjectProperty.Id, id);
        }

        Object workspaceId = null;

        workspaceId = resultSet.getObject(columnIndex++);

        object.setWorkspaceIdNull(resultSet.getBoolean(columnIndex++));

        if (workspaceId != null) {

          setBeanValue(object, _DataObjectProperty.WorkspaceId, workspaceId);
        }

        Object productDatabaseId = null;

        productDatabaseId = resultSet.getObject(columnIndex++);

        object.setProductDatabaseIdNull(resultSet.getBoolean(columnIndex++));

        if (productDatabaseId != null) {

          setBeanValue(object, _DataObjectProperty.ProductDatabaseId, productDatabaseId);
        }

        Object relateName = null;

        relateName = resultSet.getObject(columnIndex++);

        object.setRelateNameNull(resultSet.getBoolean(columnIndex++));

        if (relateName != null) {

          setBeanValue(object, _DataObjectProperty.RelateName, relateName);
        }

        Object status = null;

        status = resultSet.getObject(columnIndex++);

        object.setStatusNull(resultSet.getBoolean(columnIndex++));

        if (status != null) {

          setBeanValue(object, _DataObjectProperty.Status, status);
        }

        Object remarks = null;

        remarks = resultSet.getObject(columnIndex++);

        object.setRemarksNull(resultSet.getBoolean(columnIndex++));

        if (remarks != null) {

          setBeanValue(object, _DataObjectProperty.Remarks, remarks);
        }

        Object databaseName = null;

        databaseName = resultSet.getObject(columnIndex++);

        object.setDatabaseNameNull(resultSet.getBoolean(columnIndex++));

        if (databaseName != null) {

          setBeanValue(object, _DataObjectProperty.DatabaseName, databaseName);
        }

        Object databaseType = null;

        databaseType = resultSet.getObject(columnIndex++);

        object.setDatabaseTypeNull(resultSet.getBoolean(columnIndex++));

        if (databaseType != null) {

          setBeanValue(object, _DataObjectProperty.DatabaseType, databaseType);
        }

        Object databaseIp = null;

        databaseIp = resultSet.getObject(columnIndex++);

        object.setDatabaseIpNull(resultSet.getBoolean(columnIndex++));

        if (databaseIp != null) {

          setBeanValue(object, _DataObjectProperty.DatabaseIp, databaseIp);
        }

        Object databasePort = null;

        databasePort = resultSet.getObject(columnIndex++);

        object.setDatabasePortNull(resultSet.getBoolean(columnIndex++));

        if (databasePort != null) {

          setBeanValue(object, _DataObjectProperty.DatabasePort, databasePort);
        }

        Object databaseServiceId = null;

        databaseServiceId = resultSet.getObject(columnIndex++);

        object.setDatabaseServiceIdNull(resultSet.getBoolean(columnIndex++));

        if (databaseServiceId != null) {

          setBeanValue(object, _DataObjectProperty.DatabaseServiceId, databaseServiceId);
        }

        return object;
      } catch (Exception e) {
        throw new SQLException(e.getMessage());
      }
    }
  }
}
